﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Whoot.DI;

namespace Whoot.UI
{
    public class UIManager : IUIKit
    {
        /// <summary>
        /// 资源管理模块
        /// </summary>
        [Inject] private IAssetService _assetService;


        public IUILayerManager LayerManager { get; set; }

        /// <summary>
        /// 所有UI
        /// </summary>
        private readonly Dictionary<string, IUIPanel> _uiArray = new Dictionary<string, IUIPanel>();

        public void Start()
        {
            LayerManager = new UILayerManager();
            Debug.Log($"Reskit={_assetService?.GetType()}");

            LayerManager.SetUIKit(this);
        }

        public void Quit()
        {
        }

        #region 内部方法

        private void ShowUI(string uiName, object param = null)
        {
            IUIPanel panel = GetUI(uiName);
            panel.SetData(param);
            if (panel.IsShowing)
            {
                panel.Refresh();
            }
            else
            {
                panel.Show();
            }
        }

        #endregion


        public IUIPanel OpenUI<T>(object param = null)
        {
            Type type = typeof(T);

            return OpenUI(type.Name, type, param);
        }

        public IUIPanel OpenUI(Type type, object param = null)
        {
            return OpenUI(type.Name, type, param);
        }

        public IUIPanel OpenUI<T>(string uiName, object param = null,
            Action<IUIPanel> callback = null)
        {
            Type type = typeof(T);
            return OpenUI(uiName, type, param, callback);
        }

        public IUIPanel OpenUI(string uiName, Type type, object param = null,
            Action<IUIPanel> callback = null)
        {
            IUIPanel panel = GetUI(uiName);
            if (panel == null)
            {
                panel = Activator.CreateInstance(type) as IUIPanel;
                if (panel != null)
                {
                    Core.Inject(panel);
                    panel.SetData(param);
                    panel.Init();
                    AddUI(uiName, panel);
                }
            }

            ShowUI(uiName, param);
            return panel;
        }

        private void AddUI(string uiName, IUIPanel panel)
        {
            if (_uiArray.ContainsKey(uiName))
            {
                Debug.LogError("AddUI重复添加");
            }

            _uiArray.Add(uiName, panel);
        }

        public IUIPanel GetUI(string uiName)
        {
            IUIPanel wind = null;
            foreach (var name in _uiArray.Keys)
            {
                if (name != uiName) continue;
                wind = _uiArray[name];
                break;
            }

            return wind;
        }

        public List<string> GetAllUIName()
        {
            return this._uiArray.Keys.ToList();
        }

        public void HideUI(string uiName)
        {
            IUIPanel wind = GetUI(uiName);
            if (wind == null)
            {
                Debug.LogError($"要隐藏的界面不存在:{uiName}");
                return;
            }

            if (!wind.IsShowing)
            {
                Debug.LogError($"要隐藏的界面未打开{uiName}");
                return;
            }

            wind.Hide();
        }

        public void DestroyUI<T>()
        {
            Type type = typeof(T);
            DestroyUI(type.Name);
        }

        public void DestroyUI(Type type)
        {
            DestroyUI(type.Name);
        }

        public void DestroyUI(string uiName)
        {
            IUIPanel wind = GetUI(uiName);
            if (wind == null)
            {
                Debug.LogError($"要删除的界面不存在:{uiName}");
                return;
            }

            if (!wind.IsDotDel)
            {
                wind.Dispose();
                _uiArray.Remove(uiName);
            }
        }

        public bool IsOpenWindow(string uiName)
        {
            IUIPanel wind = GetUI(uiName);
            return wind != null && wind.IsShowing;
        }

        /// <summary>
        /// 隐藏所有窗口
        /// </summary>
        public void HideAllWindows(bool isDotDel = false)
        {
            var names = GetAllUIName();
            foreach (var uiName in names)
            {
                IUIPanel panel = GetUI(uiName);
                if (panel.IsShowing)
                {
                    if (!panel.IsDotDel || isDotDel)
                    {
                        HideUI(uiName);
                    }
                }
            }
        }

        /// <summary>
        /// 删除所有打开的窗口
        /// </summary>
        /// <param name="isMode"></param>
        public void DeleteAllWindows(bool isDotDel = false)
        {
            var names = GetAllUIName();
            foreach (var uiName in names)
            {
                DestroyUI(uiName);
            }
        }

        public void Test()
        {
            Debug.Log("测试==");
        }
    }
}